<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	public function clusters() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$data = array('title' => APP_TITLE . ' : Reference Tables - Clusters',
						  'user' => $_SESSION[CURRENT_USER_SESSION],
						  'view' => 'clusters');

			$this->load->view('html-opening');
			$this->load->view('head-opening', $data);
			$this->load->view('include-css-bootstrap');
			$this->load->view('include-css-jquery-gritter');
			$this->load->view('ie-8-supports');
			$this->load->view('include-css-ajax-loader');
			$this->load->view('include-css-jquery-live-search');
			$this->load->view('include-css-main-style');
			$this->load->view('head-closing');
			$this->load->view('body-opening');
			$this->load->view('nav-bar', $data);
			$this->load->view('container-main-opening', $data);
			$this->load->view('clusters');
			$this->load->view('footer');
			$this->load->view('container-main-closing');
			$this->load->view('include-js-jquery');
			$this->load->view('include-js-bootstrap');
			$this->load->view('include-js-docs');
			$this->load->view('include-js-application');
			$this->load->view('include-js-jquery-gritter');
			$this->load->view('include-js-gritter-setup');
			$this->load->view('include-js-back-to-top');
			$this->load->view('include-js-nav-bar-menu');
			$this->load->view('include-js-logout');
			$this->load->view('include-js-jquery-float-table-header');
			$this->load->view('include-js-jquery-live-search');
			$this->load->view('include-js-search-box');
			$this->load->view('include-js-knockout');
			$this->load->view('include-js-ko-extenders');
			$this->load->view('include-js-clusters');
			$this->load->view('body-closing');
			$this->load->view('html-closing');
		}
		else self::login();
	}

	public function districts() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$data = array('title' => APP_TITLE . ' : Reference Tables - Districts',
						  'user' => $_SESSION[CURRENT_USER_SESSION],
						  'view' => 'districts');

			$this->load->view('html-opening');
			$this->load->view('head-opening', $data);
			$this->load->view('include-css-bootstrap');
			$this->load->view('include-css-jquery-gritter');
			$this->load->view('ie-8-supports');
			$this->load->view('include-css-ajax-loader');
			$this->load->view('include-css-jquery-live-search');
			$this->load->view('include-css-main-style');
			$this->load->view('head-closing');
			$this->load->view('body-opening');
			$this->load->view('nav-bar', $data);
			$this->load->view('container-main-opening', $data);
			$this->load->view('districts');
			$this->load->view('footer');
			$this->load->view('container-main-closing');
			$this->load->view('include-js-jquery');
			$this->load->view('include-js-bootstrap');
			$this->load->view('include-js-docs');
			$this->load->view('include-js-application');
			$this->load->view('include-js-jquery-gritter');
			$this->load->view('include-js-gritter-setup');
			$this->load->view('include-js-back-to-top');
			$this->load->view('include-js-nav-bar-menu');
			$this->load->view('include-js-logout');
			$this->load->view('include-js-jquery-float-table-header');
			$this->load->view('include-js-jquery-live-search');
			$this->load->view('include-js-search-box');
			$this->load->view('include-js-knockout');
			$this->load->view('include-js-ko-extenders');
			$this->load->view('include-js-districts');
			$this->load->view('body-closing');
			$this->load->view('html-closing');
		}
		else self::login();
	}

	public function dashboard() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$data = array('title' => APP_TITLE . ' : Dasboard',
					  'user' => $_SESSION[CURRENT_USER_SESSION],
					  'view' => '');

			$this->load->view('html-opening');
			$this->load->view('head-opening', $data);
			$this->load->view('include-css-bootstrap');
			$this->load->view('ie-8-supports');
			$this->load->view('include-css-ajax-loader');
			$this->load->view('include-css-jquery-live-search');
			$this->load->view('include-css-main-style');
			$this->load->view('head-closing');
			$this->load->view('body-opening');
			$this->load->view('nav-bar', $data);
			$this->load->view('container-main-opening', $data);
			$this->load->view('dashboard');
			$this->load->view('footer');
			$this->load->view('container-main-closing');
			$this->load->view('include-js-jquery');
			$this->load->view('include-js-bootstrap');
			$this->load->view('include-js-docs');
			$this->load->view('include-js-application');
			$this->load->view('include-js-back-to-top');
			$this->load->view('include-js-nav-bar-menu');
			$this->load->view('include-js-logout');
			$this->load->view('include-js-jquery-live-search');
			$this->load->view('include-js-search-box');
			$this->load->view('include-js-highstock');
			$this->load->view('include-js-highcharts-export-module');
			$this->load->view('include-js-trending');
			$this->load->view('body-closing');
			$this->load->view('html-closing');
		}
		else self::login();
	}

	public function educationalDegrees() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$data = array('title' => APP_TITLE . ' : Reference Tables - Educational Degrees',
					  'user' => $_SESSION[CURRENT_USER_SESSION],
					  'view' => 'educational-degrees');

			$this->load->view('html-opening');
			$this->load->view('head-opening', $data);
			$this->load->view('include-css-bootstrap');
			$this->load->view('include-css-jquery-gritter');
			$this->load->view('ie-8-supports');
			$this->load->view('include-css-ajax-loader');
			$this->load->view('include-css-jquery-live-search');
			$this->load->view('include-css-main-style');
			$this->load->view('head-closing');
			$this->load->view('body-opening');
			$this->load->view('nav-bar', $data);
			$this->load->view('container-main-opening', $data);
			$this->load->view('educational-degrees');
			$this->load->view('footer');
			$this->load->view('container-main-closing');
			$this->load->view('include-js-jquery');
			$this->load->view('include-js-bootstrap');
			$this->load->view('include-js-docs');
			$this->load->view('include-js-application');
			$this->load->view('include-js-jquery-gritter');
			$this->load->view('include-js-gritter-setup');
			$this->load->view('include-js-back-to-top');
			$this->load->view('include-js-nav-bar-menu');
			$this->load->view('include-js-logout');
			$this->load->view('include-js-jquery-float-table-header');
			$this->load->view('include-js-jquery-live-search');
			$this->load->view('include-js-search-box');
			$this->load->view('include-js-knockout');
			$this->load->view('include-js-ko-extenders');
			$this->load->view('include-js-educational-degrees');
			$this->load->view('body-closing');
			$this->load->view('html-closing');
		}
		else self::login();
	}

	public function getView() {
		$view = '';

		if (!isset($_SESSION)) session_start();

		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			if (isset($_SESSION[CURRENT_PAGE_SESSION])) $view = $_SESSION[CURRENT_PAGE_SESSION];	
		}
		
		echo json_encode(array( 'view' => $view));
	}

	public function index()
	{
		date_default_timezone_set(CURRENT_TIMEZONE);
		session_start();

		if (!isset($_SESSION[CURRENT_USER_SESSION])) {
			if (!isset($_COOKIE[CURRENT_USER_SESSION])) self::login();
			else {
				$currentUser = $_COOKIE[CURRENT_USER_SESSION];
				if (!$currentUser['IsLoggedIn']) self::login();
				else self:renderView();
			}
		}
		else {
			 $currentUser = $_SESSION[CURRENT_USER_SESSION];
			if (!$currentUser['IsLoggedIn']) self::login();
			else self::renderView();
		}
	}

	public function localities() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$data = array('title' => APP_TITLE . ' : Reference Tables - Localities',
						  'user' => $_SESSION[CURRENT_USER_SESSION],
						  'view' => 'localities');

			$this->load->view('html-opening');
			$this->load->view('head-opening', $data);
			$this->load->view('include-css-bootstrap');
			$this->load->view('include-css-jquery-gritter');
			$this->load->view('ie-8-supports');
			$this->load->view('include-css-ajax-loader');
			$this->load->view('include-css-jquery-live-search');
			$this->load->view('include-css-main-style');
			$this->load->view('head-closing');
			$this->load->view('body-opening');
			$this->load->view('nav-bar', $data);
			$this->load->view('container-main-opening', $data);
			$this->load->view('localities');
			$this->load->view('footer');
			$this->load->view('container-main-closing');
			$this->load->view('include-js-jquery');
			$this->load->view('include-js-bootstrap');
			$this->load->view('include-js-docs');
			$this->load->view('include-js-application');
			$this->load->view('include-js-jquery-gritter');
			$this->load->view('include-js-gritter-setup');
			$this->load->view('include-js-back-to-top');
			$this->load->view('include-js-nav-bar-menu');
			$this->load->view('include-js-logout');
			$this->load->view('include-js-jquery-float-table-header');
			$this->load->view('include-js-jquery-live-search');
			$this->load->view('include-js-search-box');
			$this->load->view('include-js-knockout');
			$this->load->view('include-js-ko-extenders');
			$this->load->view('include-js-localities');
			$this->load->view('body-closing');
			$this->load->view('html-closing');
		}
		else self::login();
	}

	public function localityGroups() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$data = array('title' => APP_TITLE . ' : Reference Tables - Sub-Regions',
						  'user' => $_SESSION[CURRENT_USER_SESSION],
						  'view' => 'locality-groups');

			$this->load->view('html-opening');
			$this->load->view('head-opening', $data);
			$this->load->view('include-css-bootstrap');
			$this->load->view('include-css-jquery-gritter');
			$this->load->view('ie-8-supports');
			$this->load->view('include-css-ajax-loader');
			$this->load->view('include-css-jquery-live-search');
			$this->load->view('include-css-main-style');
			$this->load->view('head-closing');
			$this->load->view('body-opening');
			$this->load->view('nav-bar', $data);
			$this->load->view('container-main-opening', $data);
			$this->load->view('locality-groups');
			$this->load->view('footer');
			$this->load->view('container-main-closing');
			$this->load->view('include-js-jquery');
			$this->load->view('include-js-bootstrap');
			$this->load->view('include-js-docs');
			$this->load->view('include-js-application');
			$this->load->view('include-js-jquery-gritter');
			$this->load->view('include-js-gritter-setup');
			$this->load->view('include-js-back-to-top');
			$this->load->view('include-js-nav-bar-menu');
			$this->load->view('include-js-logout');
			$this->load->view('include-js-jquery-float-table-header');
			$this->load->view('include-js-jquery-live-search');
			$this->load->view('include-js-search-box');
			$this->load->view('include-js-knockout');
			$this->load->view('include-js-ko-extenders');
			$this->load->view('include-js-locality-groups');
			$this->load->view('body-closing');
			$this->load->view('html-closing');
		}
		else self::login();
	}

	public function login() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) self::dashboard();
		else {
			$data = array('title' => APP_TITLE,
						  'user' => null,
						  'view' => '');

			$this->load->view('html-opening');
			$this->load->view('head-opening', $data);
			$this->load->view('include-css-bootstrap');
			$this->load->view('include-css-jquery-gritter');
			$this->load->view('ie-8-supports');
			$this->load->view('include-css-ajax-loader');
			$this->load->view('include-css-main-style');
			$this->load->view('head-closing');
			$this->load->view('body-opening');
			$this->load->view('nav-bar', $data);
			$this->load->view('carousel');
			$this->load->view('container-main-opening', $data);
			$this->load->view('marketing');
			$this->load->view('footer');
			$this->load->view('container-main-closing');
			$this->load->view('include-js-jquery');
			$this->load->view('include-js-bootstrap');
			$this->load->view('include-js-docs');
			$this->load->view('include-js-application');
			$this->load->view('include-js-jquery-gritter');
			$this->load->view('include-js-gritter-setup');
			$this->load->view('include-js-back-to-top');
			$this->load->view('include-js-login');
			$this->load->view('body-closing');
			$this->load->view('html-closing');
		}
	}

	public function logon() {
		$username = $this->input->post('username');
		$password = $this->input->post('password');

		$this->load->model('users');
		$user = $this->users->getUser($username, $password);

		if ($user['IsLoggedIn']) {
			session_start();
			$_SESSION[CURRENT_USER_SESSION] = $user;
			unset($_SESSION[CURRENT_PAGE_SESSION]);
		}

		echo json_encode($user);
	}

	public function logout() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) unset($_SESSION[CURRENT_USER_SESSION]);
		if (isset($_SESSION[CURRENT_PAGE_SESSION])) unset($_SESSION[CURRENT_PAGE_SESSION]);
		if (isset($_SESSION[TRAINEE_ID_SESSION])) unset($_SESSION[TRAINEE_ID_SESSION]);
		session_destroy();

		echo json_encode(array( 'logout' => 1 ));
	}

	public function regionGroups() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$data = array('title' => APP_TITLE . ' : Reference Tables - Island Groups',
						  'user' => $_SESSION[CURRENT_USER_SESSION],
						  'view' => 'region-groups');

			$this->load->view('html-opening');
			$this->load->view('head-opening', $data);
			$this->load->view('include-css-bootstrap');
			$this->load->view('include-css-jquery-gritter');
			$this->load->view('ie-8-supports');
			$this->load->view('include-css-ajax-loader');
			$this->load->view('include-css-jquery-live-search');
			$this->load->view('include-css-main-style');
			$this->load->view('head-closing');
			$this->load->view('body-opening');
			$this->load->view('nav-bar', $data);
			$this->load->view('container-main-opening', $data);
			$this->load->view('region-groups');
			$this->load->view('footer');
			$this->load->view('container-main-closing');
			$this->load->view('include-js-jquery');
			$this->load->view('include-js-bootstrap');
			$this->load->view('include-js-docs');
			$this->load->view('include-js-application');
			$this->load->view('include-js-jquery-gritter');
			$this->load->view('include-js-gritter-setup');
			$this->load->view('include-js-back-to-top');
			$this->load->view('include-js-nav-bar-menu');
			$this->load->view('include-js-logout');
			$this->load->view('include-js-jquery-float-table-header');
			$this->load->view('include-js-jquery-live-search');
			$this->load->view('include-js-search-box');
			$this->load->view('include-js-knockout');
			$this->load->view('include-js-ko-extenders');
			$this->load->view('include-js-region-groups');
			$this->load->view('body-closing');
			$this->load->view('html-closing');
		}
		else self::login();
	}

	public function regions() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$data = array('title' => APP_TITLE . ' : Reference Tables - Regions',
						  'user' => $_SESSION[CURRENT_USER_SESSION],
						  'view' => 'regions');

			$this->load->view('html-opening');
			$this->load->view('head-opening', $data);
			$this->load->view('include-css-bootstrap');
			$this->load->view('include-css-jquery-gritter');
			$this->load->view('ie-8-supports');
			$this->load->view('include-css-ajax-loader');
			$this->load->view('include-css-jquery-live-search');
			$this->load->view('include-css-main-style');
			$this->load->view('head-closing');
			$this->load->view('body-opening');
			$this->load->view('nav-bar', $data);
			$this->load->view('container-main-opening', $data);
			$this->load->view('regions');
			$this->load->view('footer');
			$this->load->view('container-main-closing');
			$this->load->view('include-js-jquery');
			$this->load->view('include-js-bootstrap');
			$this->load->view('include-js-docs');
			$this->load->view('include-js-application');
			$this->load->view('include-js-jquery-gritter');
			$this->load->view('include-js-gritter-setup');
			$this->load->view('include-js-back-to-top');
			$this->load->view('include-js-nav-bar-menu');
			$this->load->view('include-js-logout');
			$this->load->view('include-js-jquery-float-table-header');
			$this->load->view('include-js-jquery-live-search');
			$this->load->view('include-js-search-box');
			$this->load->view('include-js-knockout');
			$this->load->view('include-js-ko-extenders');
			$this->load->view('include-js-regions');
			$this->load->view('body-closing');
			$this->load->view('html-closing');
		}
		else self::login();
	}

	public function trainees() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('trainees');
			$count = $this->trainees->recordCount();

			$data = array('title' => APP_TITLE . ' : Trainees',
						  'user' => $_SESSION[CURRENT_USER_SESSION],
						  'view' => 'trainees',
						  'count' => $count);

			$this->load->view('html-opening');
			$this->load->view('head-opening', $data);
			$this->load->view('include-css-bootstrap');
			$this->load->view('include-css-jquery-gritter');
			$this->load->view('ie-8-supports');
			$this->load->view('include-css-ajax-loader');
			$this->load->view('include-css-jquery-live-search');
			$this->load->view('include-css-main-style');
			$this->load->view('head-closing');
			$this->load->view('body-opening');
			$this->load->view('nav-bar', $data);
			$this->load->view('container-main-opening', $data);
			$this->load->view('trainees');
			$this->load->view('footer');
			$this->load->view('container-main-closing');
			$this->load->view('include-js-jquery');
			$this->load->view('include-js-bootstrap');
			$this->load->view('include-js-docs');
			$this->load->view('include-js-application');
			$this->load->view('include-js-jquery-gritter');
			$this->load->view('include-js-gritter-setup');
			$this->load->view('include-js-back-to-top');
			$this->load->view('include-js-nav-bar-menu');
			$this->load->view('include-js-logout');
			$this->load->view('include-js-jquery-float-table-header');
			$this->load->view('include-js-jquery-live-search');
			$this->load->view('include-js-search-box');
			$this->load->view('include-js-knockout');
			$this->load->view('include-js-ko-extenders');
			$this->load->view('include-js-messagebox');
			$this->load->view('include-js-trainees');
			$this->load->view('body-closing');
			$this->load->view('html-closing');
		}
		else self::login();
	}

	public function traineeInfo() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$traineeId = 0;

			if (isset($_SESSION[TRAINEE_ID_SESSION])) $traineeId = $_SESSION[TRAINEE_ID_SESSION];

			$this->load->model('trainees');
			$info = $this->trainees->getIndividualInfo($traineeId);

			$this->load->model('countries');
			$countries = $this->countries->getAll();

			$this->load->model('localities');
			$localities = $this->localities->getEverything();

			$this->load->model('districts');
			$districts = $this->districts->getAll();

			$this->load->model('educationaldegrees');
			$educationalLevels = $this->educationaldegrees->getAll();

			$data = array('title' => APP_TITLE . ' : Trainee Information' . ($traineeId <= 0? ' - New' : ''),
						  'user' => $_SESSION[CURRENT_USER_SESSION],
						  'view' => 'trainee-info',
						  'traineeId' => $traineeId,
						  'info' => $info,
						  'countries' => $countries,
						  'localities' => $localities,
						  'districts' => $districts,
						  'educationalLevels' => $educationalLevels);

			$this->load->view('html-opening');
			$this->load->view('head-opening', $data);
			$this->load->view('include-css-bootstrap');
			$this->load->view('include-css-bootstrap-datepicker');
			$this->load->view('include-css-jquery-gritter');
			$this->load->view('ie-8-supports');
			$this->load->view('include-css-ajax-loader');
			$this->load->view('include-css-jquery-live-search');
			$this->load->view('include-css-main-style');
			$this->load->view('head-closing');
			$this->load->view('body-opening');
			$this->load->view('nav-bar', $data);
			$this->load->view('container-main-opening', $data);
			$this->load->view('trainee-info', $data);
			$this->load->view('footer');
			$this->load->view('container-main-closing');
			$this->load->view('include-js-jquery');
			$this->load->view('include-js-bootstrap');
			$this->load->view('include-js-bootstrap-datepicker');
			$this->load->view('include-js-docs');
			$this->load->view('include-js-application');
			$this->load->view('include-js-jquery-gritter');
			$this->load->view('include-js-gritter-setup');
			$this->load->view('include-js-back-to-top');
			$this->load->view('include-js-nav-bar-menu');
			$this->load->view('include-js-logout');
			$this->load->view('include-js-jquery-float-table-header');
			$this->load->view('include-js-jquery-live-search');
			$this->load->view('include-js-search-box');
			$this->load->view('include-js-knockout');
			$this->load->view('include-js-ko-extenders');
			$this->load->view('include-js-open-file-dialog');
			$this->load->view('include-js-trainee-info');
			$this->load->view('body-closing');
			$this->load->view('html-closing');
		}
		else self::login();
	}

	public function trainings() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('trainings');
			$count = $this->trainings->recordCount();

			$data = array('title' => APP_TITLE . ' : Trainings',
						  'user' => $_SESSION[CURRENT_USER_SESSION],
						  'view' => 'trainings',
						  'count' => $count);

			$this->load->view('html-opening');
			$this->load->view('head-opening', $data);
			$this->load->view('include-css-bootstrap');
			$this->load->view('include-css-jquery-gritter');
			$this->load->view('ie-8-supports');
			$this->load->view('include-css-ajax-loader');
			$this->load->view('include-css-jquery-live-search');
			$this->load->view('include-css-main-style');
			$this->load->view('head-closing');
			$this->load->view('body-opening');
			$this->load->view('nav-bar', $data);
			$this->load->view('container-main-opening', $data);
			$this->load->view('trainings');
			$this->load->view('footer');
			$this->load->view('container-main-closing');
			$this->load->view('include-js-jquery');
			$this->load->view('include-js-bootstrap');
			$this->load->view('include-js-docs');
			$this->load->view('include-js-application');
			$this->load->view('include-js-jquery-gritter');
			$this->load->view('include-js-gritter-setup');
			$this->load->view('include-js-back-to-top');
			$this->load->view('include-js-nav-bar-menu');
			$this->load->view('include-js-logout');
			$this->load->view('include-js-jquery-float-table-header');
			$this->load->view('include-js-jquery-live-search');
			$this->load->view('include-js-search-box');
			$this->load->view('include-js-knockout');
			$this->load->view('include-js-ko-extenders');
			$this->load->view('include-js-messagebox');
			$this->load->view('include-js-trainings');
			$this->load->view('body-closing');
			$this->load->view('html-closing');
		}
		else self::login();
	}

	public function trainingLevels() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$data = array('title' => APP_TITLE . ' : Reference Tables - Training Levels',
						  'user' => $_SESSION[CURRENT_USER_SESSION],
						  'view' => 'training-levels');

			$this->load->view('html-opening');
			$this->load->view('head-opening', $data);
			$this->load->view('include-css-bootstrap');
			$this->load->view('include-css-jquery-gritter');
			$this->load->view('ie-8-supports');
			$this->load->view('include-css-ajax-loader');
			$this->load->view('include-css-jquery-live-search');
			$this->load->view('include-css-main-style');
			$this->load->view('head-closing');
			$this->load->view('body-opening');
			$this->load->view('nav-bar', $data);
			$this->load->view('container-main-opening', $data);
			$this->load->view('training-levels');
			$this->load->view('footer');
			$this->load->view('container-main-closing');
			$this->load->view('include-js-jquery');
			$this->load->view('include-js-bootstrap');
			$this->load->view('include-js-docs');
			$this->load->view('include-js-application');
			$this->load->view('include-js-jquery-gritter');
			$this->load->view('include-js-gritter-setup');
			$this->load->view('include-js-back-to-top');
			$this->load->view('include-js-nav-bar-menu');
			$this->load->view('include-js-logout');
			$this->load->view('include-js-jquery-float-table-header');
			$this->load->view('include-js-jquery-live-search');
			$this->load->view('include-js-search-box');
			$this->load->view('include-js-knockout');
			$this->load->view('include-js-ko-extenders');
			$this->load->view('include-js-training-levels');
			$this->load->view('body-closing');
			$this->load->view('html-closing');
		}
		else self::login();
	}

	public function renderView() {
		date_default_timezone_set('Asia/Hong_Kong');
		if (!isset($_SESSION)) session_start();

		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$page = '';
			if (isset($_SESSION[CURRENT_PAGE_SESSION])) $page = trim(strtolower($_SESSION[CURRENT_PAGE_SESSION]));
			
			if ($page == '') self::dashboard();
			else {
				if ($page == 'regions') self::regions();
				else if ($page == 'region-groups') self::regionGroups();
				else if ($page == 'locality-groups') self::localityGroups();
				else if ($page == 'localities') self::localities();
				else if ($page == 'clusters') self::clusters();
				else if ($page == 'districts') self::districts();
				else if ($page == 'educational-degrees') self::educationalDegrees();
				else if ($page == 'training-levels') self::trainingLevels();
				else if ($page == 'trainees') self::trainees();
				else if ($page == 'trainee-info') self::traineeInfo();
				else if ($page == 'trainings') self::trainings();
				else if ($page == 'users-list') self::users();
				else self::dashboard();
			}	
		}
		else self::login();
	}

	public function setTraineeId() {
		$id = $this->input->post('id');

		$traineeId = 0;

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$_SESSION[TRAINEE_ID_SESSION] = $id;
			$traineeId = $_SESSION[TRAINEE_ID_SESSION];
		}

		echo $traineeId;	
	}

	public function setView() {
		$view = $this->input->post('view');
		
		$page = '';

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$_SESSION[CURRENT_PAGE_SESSION] = $view;
			$page = $_SESSION[CURRENT_PAGE_SESSION];
		}
		
		echo $page; 
	}

	public function users() {
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$data = array('title' => APP_TITLE . ' : Administration - System Users',
						  'user' => $_SESSION[CURRENT_USER_SESSION],
						  'view' => 'users-list');

			$this->load->view('html-opening');
			$this->load->view('head-opening', $data);
			$this->load->view('include-css-bootstrap');
			$this->load->view('include-css-jquery-gritter');
			$this->load->view('ie-8-supports');
			$this->load->view('include-css-ajax-loader');
			$this->load->view('include-css-jquery-live-search');
			$this->load->view('include-css-main-style');
			$this->load->view('head-closing');
			$this->load->view('body-opening');
			$this->load->view('nav-bar', $data);
			$this->load->view('container-main-opening', $data);
			$this->load->view('users-list');
			$this->load->view('footer');
			$this->load->view('container-main-closing');
			$this->load->view('include-js-jquery');
			$this->load->view('include-js-bootstrap');
			$this->load->view('include-js-docs');
			$this->load->view('include-js-application');
			$this->load->view('include-js-jquery-gritter');
			$this->load->view('include-js-gritter-setup');
			$this->load->view('include-js-back-to-top');
			$this->load->view('include-js-nav-bar-menu');
			$this->load->view('include-js-logout');
			$this->load->view('include-js-jquery-float-table-header');
			$this->load->view('include-js-jquery-live-search');
			$this->load->view('include-js-search-box');
			$this->load->view('include-js-knockout');
			$this->load->view('include-js-ko-extenders');
			$this->load->view('include-js-users-list');
			$this->load->view('body-closing');
			$this->load->view('html-closing');
		}
		else self::login();
	}

}